package com.bifrii.bo;
import java.util.List;
import com.bifrii.dao.IPendingEventDAO;
import com.bifrii.dao.JDBCPendingEventDAO;
import com.bifrii.transferObjects.Event;
import com.bifrii.transferObjects.User;
import com.bifrii.bo.EventManager;
import com.bifrii.bo.UsersManager;
import com.bifrii.bo.CommunityManager;

public class PendingEventManager
{
	private static IPendingEventDAO pendingEvents;
	private static PendingEventManager manager;
	
	public static synchronized PendingEventManager getInstance()
	{
		if (manager == null) manager = new PendingEventManager();
		return manager;
	}
	
	private PendingEventManager()
	{
		pendingEvents = new JDBCPendingEventDAO();
	}
	
	public List<Event> getUnreadEvents(Integer userId)
	{
		return pendingEvents.selectAllPendingEvents(userId);
	}
	
	public List<Event> getUnreadEvents(User user)
	{
		return this.getUnreadEvents(user.getIdUser());
	}
	
	public void markAsRead(Integer eventId, Integer userId)
	{
		pendingEvents.delete(eventId, userId);
	}
	
	public void markAsRead(Event event, User user)
	{
		this.markAsRead(event.getIdEvent(), user.getIdUser());
	}
	
	public void markAsUnread(Integer eventId, Integer userId)
	{
		Event event = EventManager.getInstance().getEvent(eventId);
		User user = UsersManager.getInstance().getUser(userId);
		
		this.markAsUnread(event, user);
	}
	
	public void markAsUnread(Event event, User user)
	{
		pendingEvents.insert(event, user);
	}
	
	public void notifyEvent(Event event)
	{
		Integer idCommunity = event.getIdCommunity();
		List<User> communityUsers = CommunityManager.getInstance().getCommunityUsers(idCommunity);
		
		for (User user : communityUsers)
		{
			markAsUnread(event, user);
		}
	}
}
